<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 17-12-28
 * Time: 下午4:47
 */

namespace console\modules\others\controllers;


use common\models\credit_public\SlCity;
use yii\console\Controller;
use yii\db\Exception;
use yii\helpers\ArrayHelper;
use yii\httpclient\Client;

class AreaController extends Controller
{
    /**
     * php yii others/area/get-city2-by-c-order
     */
    public function _actionGetCity2ByCOrder()
    {
        $client = new Client();
        $citys = SlCity::find()->where(['and', ['display'=>3], ['>', 'c_order', 0]])->all();
        $trans = \Yii::$app->db_public->beginTransaction();
        try{
            $batch_input = [];
            foreach ($citys as $k => $v){
                $c = clone $client;
                $r = $c->createRequest()
                    ->setUrl("http://d.jd.com/area/get")
                    ->setMethod('get')
                    ->setData(['fid'=>$v->c_order])
                    ->send();
                $data = json_decode($r->content);
                $data = ArrayHelper::toArray($data);
                array_multisort(array_column($data,'id'),SORT_ASC, $data);
                foreach ($data as $k1 => $v1){
                    $x['state'] = $v->state;
                    $x['city'] = $v1['name'];
                    $x['sz_code'] = substr($v->sz_code, 0, 2).str_pad($k1+1, 2, '0', STR_PAD_LEFT).'00';
                    $x['display'] = 2;
                    $x['c_order'] = $v1['id'];
                    $batch_input[] = $x;
                }
            }
            \Yii::$app->db_public->createCommand()->batchInsert(SlCity::tableName(), ['state', 'city', 'sz_code', 'display', 'c_order'], $batch_input)->execute();
            $trans->commit();
        }catch (Exception $e){
            $trans->rollBack();
            throw $e;
        }
    }

    /**
     * php yii others/area/get-city1-by-c-order
     */
    public function _actionGetCity1ByCOrder()
    {
        $client = new Client();
        $citys = SlCity::find()->where(['and', ['display'=>2], ['>', 'c_order', 0]])->all();
        $trans = \Yii::$app->db_public->beginTransaction();
        try{
            $batch_input = [];
            foreach ($citys as $k => $v){
                $c = clone $client;
                $r = $c->createRequest()
                    ->setUrl("http://d.jd.com/area/get")
                    ->setMethod('get')
                    ->setData(['fid'=>$v->c_order])
                    ->send();
                $data = json_decode($r->content);
                $data = ArrayHelper::toArray($data);
                array_multisort(array_column($data,'id'),SORT_ASC, $data);
                foreach ($data as $k1 => $v1){
                    $x['state'] = $v->state;
                    $x['city'] = $v1['name'];
                    $x['sz_code'] = substr($v->sz_code, 0, 4).str_pad($k1+1, 2, '0', STR_PAD_LEFT);
                    $x['display'] = 1;
                    $x['c_order'] = $v1['id'];
                    $batch_input[] = $x;
                }
            }
            \Yii::$app->db_public->createCommand()->batchInsert(SlCity::tableName(), ['state', 'city', 'sz_code', 'display', 'c_order'], $batch_input)->execute();
            $trans->commit();
        }catch (Exception $e){
            $trans->rollBack();
            throw $e;
        }
    }
}